<?php
require("classes/Helpers.php");

// remove the magic quotes, because magic quotes f!cking suck
if(get_magic_quotes_gpc ()) {
    if (!empty($_GET))     Helpers::remove_magic_quotes( $_GET );
    if (!empty($_POST))    Helpers::remove_magic_quotes( $_POST );
    if (!empty($_COOKIE))  Helpers::remove_magic_quotes( $_COOKIE );
    //if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
}


$do  = $_GET['do'];

require ("classes/init.php");
$dbl = $PARAPARA->dblayer;

// do login gets special treatment /////////////////////////
if($do == 'login') {
    $screenname = trim($_POST['c1']);
    $password   = $_POST['c2'];
    if( strlen( $screenname ) && strlen( $password ) ) {
       $account = $dbl->account_fetch_by_screenname( $screenname );
       if($account) {
           $pwd_md5 = md5( $password );
           if($account->check_authentication( $screenname, $pwd_md5 ) ) {
               $PARAPARA->set_auth_cookie( $screenname, $pwd_md5 ); 
           }
       }
    }

    header('Location: admin.php');
    return;
}

if($do == 'logout') {
    $PARAPARA->delete_auth_cookie();
    header('Location: admin.php');
    return;
}


$account = $PARAPARA->fetch_account();

if( !$PARAPARA->is_admin() ) {
    require ( 'admin/ix_head.php' );
    require ( 'admin/loginbox.php');
    require ( 'admin/ix_foot.php' );

    return;
}

// renew the token
$PARAPARA->renew_auth_cookie();


// section chooser ////////////////////////////////////////
$sec = $_GET['sec'];
if(!$sec)
    $sec = 'main';


require ( 'admin/ix_head.php' );
require ( 'admin/logoutbox.php' );
require ( 'admin/topmenu.php' );


switch ($sec) {
case 'main': 
    require( 'admin/main.php' ); break;
case 'chat':
    require( 'admin/chat.php'); break;
case 'forum':
    require( 'admin/forum.php'); break;
case 'acc':
    require( 'admin/accounts.php'); break;
case 'sett':
    require( 'admin/settings.php'); break;
case 'link':
    require( 'admin/linkroll.php' ); break;
case 'ipblock':
    require( 'admin/ipblocks.php' ); break;
case 'quot':
    require( 'admin/quotes.php'); break;

default:
    require( 'admin/main.php' );
}

require ( 'admin/ix_foot.php' );

function padm_infostatus( $info ) {
    echo "<div class=\"padm_infostatus\">", $info, "</div>\n";
}

function padm_info( $info ) {
    echo "<div class=\"padm_info\">", $info, "</div>\n";
}

function padm_warn( $warn ) {
    echo "<div class=\"padm_warn\">", $warn, "</div>\n";
}

function padm_dump( $object ) {
    echo "<pre style=\"text-align: left; border: 1px solid black\">", var_dump( $object), "</pre>\n";
}

?>